package com.softserveinc.autorent.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import com.softserveinc.autorent.manager.CarModelEJB;
import com.softserveinc.autorent.model.CarModel;


@ManagedBean
@RequestScoped
public class CarModelMB  implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3081686528960088139L;
	@EJB
	private CarModelEJB carModelEJB;
	private List<CarModel> list = new ArrayList<CarModel>();
	private CarModel carModel = new CarModel();

	public List<CarModel> getList() {			   	    			
		return findAll();
	}

	public List<CarModel> findAll() {			
    	list = carModelEJB.findAll();    			
		return list;
	}

	public List<CarModel> findById(int id) {			
    	list = carModelEJB.findById(id);    			
		return list;
	}
	
	public List<CarModel> findAllByCarVendorId(int id) {			
    	list = carModelEJB.findAllByCarVendorId(id);    			
		return list;
	}
	
	
	public String doDelete(int id)
	{
		System.out.println("Try to delete model id="+id);
		if (list.size() > 0)
		{
			carModelEJB.delete(id);
		}
		return null;//"car_model_list.xhtml?faces-redirect=true";
	}

	public String doChoose(int id)
	{	
		System.out.println("Try to choose vendor id="+id);		
		if (list.size() > 0)
		{
			for (CarModel model : list) {
				if (model.getId() == id)
				this.carModel = model;
			}
		}
		return "edit_car_model.xhtml?faces-redirect=true";// - убирает сессию, здесь надо передать данные
	}
	
		

	public String doUpdate(int id)
	{
		carModel.setId(id);
		carModelEJB.update(carModel);		
		return "car_model_list.xhtml?faces-redirect=true";		
	}
	
	public String doCreate()
	{
		System.out.println("Try to update object w name="+carModel.getName());		
		carModelEJB.create(carModel);	
		return "car_model_list.xhtml?faces-redirect=true";
	}	
	

	public CarModel getCarModel() {
		return carModel;
	}
	public void setCarModel(CarModel carModel) {
		this.carModel = carModel;
	}
	public void setList(List<CarModel> list) {
		this.list = list;
	}
}